package com.wzdigit.wms.wms.service.basic;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzdigit.framework.Shift;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateZoneGroupRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchZoneGroupRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusZoneGroupRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateZoneGroupRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.ZoneGroupApiResponse;
import com.wzdigit.wms.basic.client.response.basic.ZoneGroupResponse;
import com.wzdigit.wms.basic.domain.basic.ZoneGroup;
import com.wzdigit.wms.basic.enums.BaseIsEnabledEnum;
import com.wzdigit.wms.basic.mapper.core.basic.ZoneGroupMapper;
import com.wzdigit.wms.basic.util.HttpSessionUtil;
import com.wzdigit.wms.common.entity.StatusCode;
import net.dreamlu.mica.core.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 区域 服务类
 * @author yangdongjie
 * @ZoneGroupAttrService.java
 * @2021年1月4日上午11:16:31
 */
@Service
public class ZoneGroupService {

    @Autowired
    private ZoneGroupMapper zoneGroupMapper;

	public List<ZoneGroupApiResponse> list() {
		return zoneGroupMapper.selectAllByStatus(BaseIsEnabledEnum.USE.code);
	}

	public List<ZoneGroupApiResponse> listAllByWhsId(Integer whsId) {
		return zoneGroupMapper.selectAllByWhsId(whsId);
	}
    
    public ZoneGroupResponse getZoneGroup(Integer zoneGroupId) {
    	return zoneGroupMapper.selectOneByZoneGroupId(zoneGroupId);
    }
    
    @SuppressWarnings("rawtypes")
	public <T> PagerInfo<List<ZoneGroupResponse>> searchZoneGroup(SearchZoneGroupRequest dto) {
    	Page<T> page = PageHelper.offsetPage(dto.getSkipCount(), dto.getMaxResultCount());
        PagerInfo<List<ZoneGroupResponse>> pagerInfo = new PagerInfo<List<ZoneGroupResponse>>();
        List<ZoneGroupResponse> list = zoneGroupMapper.selectAll(dto);
        pagerInfo.setItems(list);
        pagerInfo.setTotalCount(page.getTotal());
		return pagerInfo;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public ZoneGroupResponse createZoneGroup(CreateZoneGroupRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	ZoneGroup zoneGroup = zoneGroupMapper.selectOneByZoneGroupCode(dto.getZoneGroupCode());
    	if(zoneGroup != null) {
    		// 编码已存在
    		Shift.fatal(StatusCode.CODE_DATA_EXIST);
    	}
    	zoneGroup = new ZoneGroup();
    	BeanUtil.copyNonNull(dto, zoneGroup);
    	zoneGroup.setCreateUser(loginName);
    	zoneGroup.setCreateTime(new Date());
    	zoneGroupMapper.insert(zoneGroup);
    	
    	ZoneGroupResponse response = new ZoneGroupResponse();
    	BeanUtil.copyNonNull(zoneGroup, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public ZoneGroupResponse updateZoneGroup(UpdateZoneGroupRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	ZoneGroup zoneGroup = zoneGroupMapper.selectOneByZoneGroupCode(dto.getZoneGroupCode());
    	if(zoneGroup != null) {
    		if (dto.getZoneGroupId().intValue() != zoneGroup.getZoneGroupId().intValue()) {
    			// 编码已存在
    			Shift.fatal(StatusCode.CODE_DATA_EXIST);
			}
    	}
    	zoneGroup = zoneGroupMapper.selectByPrimaryKey(dto.getZoneGroupId());
    	BeanUtil.copy(dto, zoneGroup);
    	zoneGroup.setModifyUser(loginName);
    	zoneGroup.setModifyTime(new Date());
    	zoneGroupMapper.updateById(zoneGroup);
    	
    	ZoneGroupResponse response = new ZoneGroupResponse();
    	BeanUtil.copy(zoneGroup, response);
		return response;
    }
    
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public <T> Result<T> updateStatus(UpdateStatusZoneGroupRequest dto,
    		HttpServletRequest servletRequest) {
    	String loginName = HttpSessionUtil.getLoginName(servletRequest);
    	ZoneGroup zoneGroup = zoneGroupMapper.selectByPrimaryKey(dto.getZoneGroupId());
    	zoneGroup.setStatus(dto.getStatus());
    	zoneGroup.setModifyUser(loginName);
    	zoneGroup.setModifyTime(new Date());
    	zoneGroupMapper.updateStatus(zoneGroup);
		return new Result<>();
    }
}
